from flask import Flask, render_template, Blueprint, send_file,json,jsonify
from flask import request, jsonify
from flask_sqlalchemy import SQLAlchemy
import MySQLdb
from config import dbconfig

from urllib.parse import quote

app = Flask(__name__)
app.config['DEBUG']=True
app.config['SQLALCHEMY_DATABASE_URI'] ='mysql://root@localhost:3306/appsql'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
# app.config.from_object(dbconfig)
db = SQLAlchemy(app)
db.init_app(app=app)

class Announcement (db.Model):
    __tablename__ = 'announcement'
    notice=db.Column(db.VARCHAR(1000),primary_key=True)


def query_announcement():
    result=Announcement.query.all()
    print(type(result))
    print(result)
    print(type(result[0]))
    return result

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/main1')
def main1():
    return render_template('main.html')

@app.route('/log')
def log():
    return render_template('log.html')

@app.route('/feedback')
def feedback():
    return render_template('feedback.html')

@app.route('/roadbus')
def roadbus():
    return render_template('roadbus.html')

@app.route('/bustime')
def bustime():
    return render_template('bustime.html')

@app.route('/announcement',methods=["GET","POST"])
def announcement():
    if request.method=='POST':
        notice = request.get_data()
        notice=notice.decode()
        try:
            admin = Announcement(notice=notice)
            db.session.add(admin)
            db.session.commit()
            print('finally success!!!')
        except Exception as e:
            db.session.rollback()
            print('no')
            print(e)
            raise e
        return render_template('announcement.html')
        print(notice)
    elif request.method=='GET':
        data=query_announcement()
        return render_template('announcement.html',back_data=data)


if __name__ == '__main__':
    app.run()
